const express = require('express');
const multer = require('multer');
const path = require('path')
const app = express();
const { uploadFile, getFileList, delFile, clsFile } = require('./server/upload.js')

const imagesFolderPath = '/cloudFiles' // 静态资源文件夹
app.use(imagesFolderPath, express.static(path.join(__dirname, 'cloudFiles')));
app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

// Configure Multer for file upload
const upload = multer({ storage: multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, imagesFolderPath)); // Specify the destination directory where files will be saved
    },
    filename: function (req, file, cb) {
        const uniqueFileName = file.originalname.replace(/\s/g, '');; // Generate a unique filename
        cb(null, uniqueFileName);
    }
}) });

// 处理文件上传请求
app.post('/upload', upload.single('file'), uploadFile)
app.get('/files/list', getFileList);
app.get('/files/del', delFile);
app.get('/files/cls', clsFile);


// 启动服务器
const port = 3000; // 可自定义端口号
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
    console.log(`http://localhost:${port}`);
});
